﻿package org.ilanchong.droidcontroller;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import org.ilanchong.droidcontroller.R;
import org.ilanchong.droidcontroller.ui.UpdateCommandUI;
import org.ilanchong.droidcontroller.ui.UpdateComputerMessageUI;

public class DroidControllerActivity extends Activity implements OnClickListener {
	private final int COMMAND_PORT = 30000;
	
	private int mButtonsId [] = new int[]{
			R.id.btn_connect_pc,
			R.id.btn_connect_wifi,
			R.id.btn_control_logoff,
			R.id.btn_control_reboot,
			R.id.btn_control_shutdown
	};
	
	private TextView mStatusTextView;
	private LinearLayout mLayout = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initUI();
    }
    
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.btn_connect_pc:
			UpdateComputerMessageUI task = new UpdateComputerMessageUI(this);
			BroadCastCommand arg = new BroadCastCommand(Utils.BROADCAST_IP, Utils.BROADCAST_PORT, BroadCastCommand.CMD_GET_IP_HOSTNAME);	
			task.execute(arg);
			break;
		case R.id.btn_connect_wifi:
			// nothing
			mStatusTextView.setText(R.string.set_wifi_net);
			Toast.makeText(DroidControllerActivity.this, R.string.set_wifi_net, Toast.LENGTH_SHORT).show();
			break;
		case R.id.btn_control_logoff:
			sendCommand(ControlCommand.CMD_LOGOFF);
			break;
		case R.id.btn_control_reboot:
			sendCommand(ControlCommand.CMD_REBOOT);
			break;
		case R.id.btn_control_shutdown:
			sendCommand(ControlCommand.CMD_SHUTDOWN);
			break;
		default:
				
		}
	}
	
    
    private void initUI(){
    	mStatusTextView = (TextView) findViewById(R.id.txt_status);
    	mLayout = (LinearLayout) findViewById(R.id.linearlayout_layout);
    	
    	for (int i = 0; i < mButtonsId.length; i ++){
        	Button button = (Button) findViewById(mButtonsId[i]);
        	button.setOnClickListener(this);
        }
    }
    
    private void sendCommand(String cmd){
    	UpdateCommandUI task = new UpdateCommandUI(DroidControllerActivity.this);
    	// 获取电脑，并选择发送命令
		List<ComputerInfo> cList = null;
		if(cList==null || cList.size()==0){
			mStatusTextView.setText(R.string.no_computer);
		}else {
			ControlCommand arg = new ControlCommand(cList, COMMAND_PORT, cmd);
			task.execute(arg);
		}
    }

	public TextView getStatusView(){
    	if (mStatusTextView == null){
    		mStatusTextView = (TextView) findViewById(R.id.txt_status);
    	}
    	return mStatusTextView;
    }
    
    public LinearLayout getLinearLayout(){
    	if (mLayout == null){
    		mLayout = (LinearLayout) findViewById(R.id.linearlayout_layout);
    	}
    	return mLayout;
    }


}